<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单号" prop="workOrderId">
        <el-input
          v-model="queryParams.workOrderId"
          placeholder="请输入工单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单类型" prop="workOrderType">
        <el-select v-model="queryParams.workOrderType" placeholder="请选择工单类型" clearable>
          <el-option
            v-for="dict in dict.type.work_order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="第三方系统单号" prop="thirdSysNumber">
        <el-input
          v-model="queryParams.thirdSysNumber"
          placeholder="请输入第三方系统单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本单客户联系人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="请输入本单客户联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本单客户联系电话" prop="contactNumber">
        <el-input
          v-model="queryParams.contactNumber"
          placeholder="请输入本单客户联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的店铺编号" prop="storeNumber">
        <el-input
          v-model="queryParams.storeNumber"
          placeholder="请输入关联的店铺编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的店铺名" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入关联的店铺名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单状态" prop="workOrderStatusId">
        <el-select v-model="queryParams.workOrderStatusId" placeholder="请选择工单状态" clearable>
          <el-option
            v-for="dict in dict.type.work_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工单响应超时" prop="workOrderResponseTimeOver">
        <el-input
          v-model="queryParams.workOrderResponseTimeOver"
          placeholder="请输入工单响应超时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单上门超时" prop="workOrderOnsiteTimeOver">
        <el-input
          v-model="queryParams.workOrderOnsiteTimeOver"
          placeholder="请输入工单上门超时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单处理超时" prop="workOrderProcessingTimeOver">
        <el-input
          v-model="queryParams.workOrderProcessingTimeOver"
          placeholder="请输入工单处理超时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的项目编号" prop="projectNumber">
        <el-input
          v-model="queryParams.projectNumber"
          placeholder="请输入关联的项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的项目名" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入关联的项目名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单子状态" prop="workOrderStatusIdChild">
        <el-select v-model="queryParams.workOrderStatusIdChild" placeholder="请选择工单子状态" clearable>
          <el-option
            v-for="dict in dict.type.work_order_status_child"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:workOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:workOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:workOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:workOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工单号" align="center" prop="workOrderId" v-if="true"/>
      <el-table-column label="工单类型" align="center" prop="workOrderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_order_type" :value="scope.row.workOrderType"/>
        </template>
      </el-table-column>
      <el-table-column label="第三方系统单号" align="center" prop="thirdSysNumber" />
      <el-table-column label="本单客户联系人" align="center" prop="contacts" />
      <el-table-column label="本单客户联系电话" align="center" prop="contactNumber" />
      <el-table-column label="关联的店铺主键" align="center" prop="storeId" />
      <el-table-column label="关联的店铺编号" align="center" prop="storeNumber" />
      <el-table-column label="关联的店铺名" align="center" prop="storeName" />
      <el-table-column label="关联的店铺类型" align="center" prop="storeType" />
      <el-table-column label="关联的店铺地址" align="center" prop="storeAddress" />
      <el-table-column label="工单描述" align="center" prop="workOrderDescription" />
      <el-table-column label="工单状态" align="center" prop="workOrderStatusId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_order_status" :value="scope.row.workOrderStatusId"/>
        </template>
      </el-table-column>
      <el-table-column label="工单响应超时" align="center" prop="workOrderResponseTimeOver" />
      <el-table-column label="工单上门超时" align="center" prop="workOrderOnsiteTimeOver" />
      <el-table-column label="工单处理超时" align="center" prop="workOrderProcessingTimeOver" />
      <el-table-column label="关联的项目编号" align="center" prop="projectNumber" />
      <el-table-column label="关联的项目名" align="center" prop="projectName" />
      <el-table-column label="工单子状态" align="center" prop="workOrderStatusIdChild">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_order_status_child" :value="scope.row.workOrderStatusIdChild"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:workOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:workOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工单池对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工单类型" prop="workOrderType">
          <el-select v-model="form.workOrderType" placeholder="请选择工单类型">
            <el-option
              v-for="dict in dict.type.work_order_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="第三方系统单号" prop="thirdSysNumber">
          <el-input v-model="form.thirdSysNumber" placeholder="请输入第三方系统单号" />
        </el-form-item>
        <el-form-item label="本单客户联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入本单客户联系人" />
        </el-form-item>
        <el-form-item label="本单客户联系电话" prop="contactNumber">
          <el-input v-model="form.contactNumber" placeholder="请输入本单客户联系电话" />
        </el-form-item>
        <el-form-item label="关联的店铺编号" prop="storeNumber">
          <el-input v-model="form.storeNumber" placeholder="请输入关联的店铺编号" />
        </el-form-item>
        <el-form-item label="关联的店铺名" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入关联的店铺名" />
        </el-form-item>
        <el-form-item label="关联的店铺地址" prop="storeAddress">
          <el-input v-model="form.storeAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工单描述" prop="workOrderDescription">
          <el-input v-model="form.workOrderDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工单状态" prop="workOrderStatusId">
          <el-select v-model="form.workOrderStatusId" placeholder="请选择工单状态">
            <el-option
              v-for="dict in dict.type.work_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工单子状态" prop="workOrderStatusIdChild">
          <el-select v-model="form.workOrderStatusIdChild" placeholder="请选择工单子状态">
            <el-option
              v-for="dict in dict.type.work_order_status_child"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWorkOrder, getWorkOrder, delWorkOrder, addWorkOrder, updateWorkOrder } from "@/api/system/workOrder";

export default {
  name: "WorkOrder",
  dicts: ['work_order_processing', 'work_order_status_child', 'work_order_type', 'work_order_status'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单池表格数据
      workOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workOrderId: undefined,
        workOrderType: undefined,
        thirdSysNumber: undefined,
        contacts: undefined,
        contactNumber: undefined,
        storeId: undefined,
        storeNumber: undefined,
        storeName: undefined,
        storeType: undefined,
        storeAddress: undefined,
        workOrderDescription: undefined,
        processing: undefined,
        workOrderStatusId: undefined,
        workOrderResponseTimeOver: undefined,
        workOrderOnsiteTimeOver: undefined,
        workOrderProcessingTimeOver: undefined,
        projectId: undefined,
        projectNumber: undefined,
        projectName: undefined,
        workOrderStatusIdChild: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workOrderId: [
          { required: true, message: "工单号不能为空", trigger: "blur" }
        ],
        workOrderType: [
          { required: true, message: "工单类型不能为空", trigger: "change" }
        ],
        contacts: [
          { required: true, message: "本单客户联系人不能为空", trigger: "blur" }
        ],
        contactNumber: [
          { required: true, message: "本单客户联系电话不能为空", trigger: "blur" }
        ],
        storeId: [
          { required: true, message: "关联的店铺主键不能为空", trigger: "change" }
        ],
        workOrderDescription: [
          { required: true, message: "工单描述不能为空", trigger: "blur" }
        ],
        workOrderStatusId: [
          { required: true, message: "工单状态不能为空", trigger: "change" }
        ],
        projectId: [
          { required: true, message: "关联的项目id不能为空", trigger: "change" }
        ],
        projectNumber: [
          { required: true, message: "关联的项目编号不能为空", trigger: "blur" }
        ],
        projectName: [
          { required: true, message: "关联的项目名不能为空", trigger: "blur" }
        ],
        workOrderStatusIdChild: [
          { required: true, message: "工单子状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工单池列表 */
    getList() {
      this.loading = true;
      listWorkOrder(this.queryParams).then(response => {
        this.workOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workOrderId: undefined,
        workOrderType: undefined,
        thirdSysNumber: undefined,
        contacts: undefined,
        contactNumber: undefined,
        storeId: undefined,
        storeNumber: undefined,
        storeName: undefined,
        storeType: undefined,
        storeAddress: undefined,
        workOrderDescription: undefined,
        workOrderStatusId: undefined,
        workOrderStatus: undefined,
        workOrderResponseTimeOver: undefined,
        workOrderOnsiteTimeOver: undefined,
        workOrderProcessingTimeOver: undefined,
        projectId: undefined,
        projectNumber: undefined,
        projectName: undefined,
        updateTime: undefined,
        updateBy: undefined,
        createTime: undefined,
        createBy: undefined,
        delFlag: undefined,
        workOrderStatusIdChild: undefined,
        workOrderStatusChild: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workOrderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工单池";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const workOrderId = row.workOrderId || this.ids
      getWorkOrder(workOrderId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工单池";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.workOrderId != null) {
            updateWorkOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addWorkOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workOrderIds = row.workOrderId || this.ids;
      this.$modal.confirm('是否确认删除工单池编号为"' + workOrderIds + '"的数据项？').then(() => {
        this.loading = true;
        return delWorkOrder(workOrderIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/workOrder/export', {
        ...this.queryParams
      }, `workOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
